<template>
  <el-dialog append-to-body :title="title" :visible.sync="dialogFormVisible" width="30vw" @close="close">
    <el-form ref="form" label-width="auto" :model="form" :rules="rules">
      <el-form-item v-if="title == '添加'" label="设备ID" prop="clientId">
        <el-input v-model="form.ClientId" autocomplete="off" />
      </el-form-item>
      <el-form-item v-if="title == '编辑'" label="设备ID" prop="clientId">
        <el-input v-model="form.ClientId" autocomplete="off" :disabled="true" />
      </el-form-item>
      <el-form-item label="密码" prop="clientPsw">
        <el-input v-model="form.ClientPsw" autocomplete="off" />
      </el-form-item>
      <el-form-item label="备注信息" prop="memo">
        <el-input v-model="form.Memo" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save(form)">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { DeviceAdd, DeviceUdp } from '@/api/MqttLinker'
  export default {
    name: 'UserEdit',
    data() {
      return {
        form: {
          ClientId: '',
          ClientPsw: '',
          Memo: '',
          clientId: '',
          clientPsw: '',
          memo: '',
        },
        toform: {
          clientId: '',
          clientPsw: '',
          memo: '',
        },
        rules: {
          ClientId: [{ required: true, trigger: 'blur', message: '请输入设备ID' }],
          ClientPsw: [{ required: true, trigger: 'blur', message: '请输入密码' }],
          Memo: [{ required: true, trigger: 'blur', message: '请输入备注信息' }],
        },
        title: '',
        dialogFormVisible: false,
        options: [],
      }
    },
    created() {},
    methods: {
      async showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.toform = Object.assign({}, row)
          console.log(this.toform)
          this.form.ClientId = this.toform.clientId
          this.form.ClientPsw = this.toform.clientPsw
          this.form.Memo = this.toform.memo
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      changeData(form) {
        if (this.title == '添加') {
          delete form.clientId
          delete form.clientPsw
          delete form.memo
        } else {
          this.form.clientId = this.form.ClientId
          this.form.clientPsw = this.form.ClientPsw
          this.form.memo = this.form.Memo
          delete form.ClientId
          delete form.ClientPsw
          delete form.Memo
        }
        return form
      },
      save(form) {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let box = await this.changeData(form)
            let data
            console.log(box)
            if (this.title == '添加') {
              data = await DeviceAdd(box)
            } else if (this.title == '编辑') {
              data = await DeviceUdp(box)
            }
            if (data.code == 1000) {
              this.$baseMessage(`${this.title}成功`)
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      async succestlist(data) {
        this.form.avatarUrl = await data[0].response.data
      },
      updatefileList(data) {
        this.form.iconImg = data
      },
    },
  }
</script>
<style lang="scss" scoped>
  .el-form-item ::v-deep .el-select {
    width: 100%;
  }
</style>
